(function($, doc) {
	
	$.plusReady(function() {

		var user = app.getState();
		var uid = user.user_id;
		var postpass = md5(user.password);
		var sysCoin = "GAT";
		var currentCoin = 'eth';
		var coindata;
		
		var myWallet;
		var pricelist;
		var mycoinlist;
		var pagesize = 5;
		var page = 1;
		
		
		var pullkeys = [1, 0, 0];

		var scrollpos = 0;
		var isLoading = false;
		
		
		// modify by yaozy 事件绑定相关的代码放到前面以解决未创建钱包时打开子页面无法返回的问题

		window.addEventListener('refreshList', function(event) {
			refresList();
		});
		/*
		doc.getElementById('showdown').addEventListener('tap', function(){
			$('#middlePopover').popover('show');
		});*/

		$("#marketlist").on("tap", "a", function() {
			currentCoin = this.id;
			renderPrice();
		}, false);

		$("#mlist").on('tap', "li", function() {

			$.openWindow({
				url: 'pages/market/detail.html',
				id: 'marketdetail',
				extras: {
					token: currentCoin,
					dataid: this.id
				},
				createNew: true
			});
		});

		$("#ordersarea").on("tap", "a", function() {
			var href = this.getAttribute('href');
			var price = doc.getElementById('currentprice').innerHTML;
			$.openWindow({
				url: href,
				id: this.id,
				extras: {
					token: currentCoin,
					price: price
				},
				createNew: true
			});
		}, false);
		
		
		doc.getElementById('jilu_left').addEventListener('tap', function() {
			pullkeys[0] = 1;
		});

		doc.getElementById('jilu_right').addEventListener('tap', function() {
			pullkeys[0] = 2;
			
			if (!getSaleRecord.excuted) {
				getSaleRecord()
				getSaleRecord.excuted = true;
			}
		});
		
		doc.getElementById('buyarea').addEventListener('touchend', touchendHandler);
		doc.getElementById('salearea').addEventListener('touchend', touchendHandler);
		
	
		function touchendHandler() {
			var index = pullkeys[0];
			var records = pullkeys[index];
				
			if (!isLoading && records > 0) {
				var item = this.lastElementChild;
					
				if (item.offsetTop + item.offsetHeight + 20 > this.clientHeight) {
					var page = (records / 20 | 0) + 1;
					
					if (index === 2) {
						getSaleRecord(page);
					} else {
						getBuyRecord(page);
					}
				}
			}
		}
		

		myWallet = ethscan.getWallet();
		pricelist = ethscan.getpriceList();
		mycoinlist = ethscan.getmycoinlist();
		
		
		$.init();
		
		
		//console.log(JSON.stringify(mycoinlist.length));
		//console.log(JSON.stringify(pricelist));
		doc.getElementById('syscoin').innerHTML = sysCoin;
		
		var url = weburl + "/home/ChatApi/userinfo.html?u=" + uid + "&p=" + postpass;

		//console.log(url);
		mui.getJSON(url, function(res) {
			if(res.status == 1) {
				doc.getElementById("gatcmoney").innerHTML = res.data.a1_money;
			}
		});

		function renderSelect() {
			var marketliststr = '';
			i = 0;
			for(var p in mycoinlist) {
				i++;
				marketliststr += '<li class="mui-table-view-cell"><a href="#" id="' + p + '">' + sysCoin + '/' + mycoinlist[p].name + '</a></li>';
			}
			doc.getElementById('marketlist').innerHTML = marketliststr;
			doc.getElementById('middlePopover').style.height = 10 + 45 * i + 'px';
		}
		
		renderSelect();

		function toDecimal(x) {
			var f = parseFloat(x);
			if(isNaN(f)) {
				return;
			}
			f = Math.round(x * 100) / 100;
			return f;
		}

		function renderPrice() {
			coindata = mycoinlist[currentCoin];
			var coinname = coindata.name;
			doc.getElementById('mytitle').innerHTML = sysCoin + '/' + coinname;
			doc.getElementById('currentcoin').innerHTML = coinname;
			doc.getElementById('currentcoin2').innerHTML = coinname;
			var price = pricelist[coinname];
			var price = price ? price * 1 : 1;
			var price = toDecimal(price);
			var pricetop = toDecimal(price * 1.1);
			var pricelow = toDecimal(price * 0.9);
			//console.log(price);
			//console.log(pricetop);
			//console.log(pricelow);
			doc.getElementById('currentprice').innerHTML = price;
			doc.getElementById('currentpricetop').innerHTML = pricetop;
			doc.getElementById('currentpricelow').innerHTML = pricelow;
			doc.getElementById('saletitle').innerHTML = '出售' + coinname;
			doc.getElementById('buytitle').innerHTML = '购买' + coinname;
			doc.getElementById('ordertitle').innerHTML = coinname + '订单';
			if(coindata['ctype'] == 'ethcontact') {
				coindata['abi'] = ethscan.getabi(currentCoin);
			}
			mui('#middlePopover').popover('hide');
			ethscan.getAmount(coindata, function(money) {
				doc.getElementById('currentmoney').innerHTML = money;
			});
			
			refresList();
		}
		
		renderPrice();
		
		
		function refresList() {			
			pullkeys[1] = pullkeys[2] = 0;
			getBuyRecord(0, true);
			
			if (getSaleRecord.excuted) {
				getSaleRecord(0, true);
			}
		}


		function getBuyRecord(page, reset) {
			showMessage(true);
			page = page > 1 ? '&p=' + page + '&size=20' : '';
			
			$.getJSON(weburl + 'Home/ChatApi/getBuyTrades?ctype=' + currentCoin + page, function(res) {
				var list = res.buylist;
				
				if(list.length > 0) {
					insertHtml(doc.getElementById('buyarea'), template('buytpl', {
						data: list,
						currentname: coindata.name,
						syscoinname: sysCoin
					}), reset);
				} else {
					doc.getElementById('buyarea').innerHTML = '';
				}
				
				if (list && list.length === 20) {
					pullkeys[1] += 20;
				} else {
					pullkeys[1] = 0;
				}
				
				showMessage(false);
			});
		}

			
		function getSaleRecord(page, reset) {
			showMessage(true);
			page = page > 1 ? '&p=' + page + '&size=20' : '';
			
			$.getJSON(weburl + 'Home/ChatApi/getSaleTrades?ctype=' + currentCoin + page, function(res) {
				var list = res.salelist;
				
				if(list.length > 0) {
					insertHtml(doc.getElementById('salearea'), template('saletpl', {
						data: list,
						currentname: coindata.name,
						syscoinname: sysCoin
					}), reset);
				} else {
					doc.getElementById('salearea').innerHTML = '';
				}
				
				if (list && list.length === 20) {
					pullkeys[2] += 20;
				} else {
					pullkeys[2] = 0;
				}
				
				showMessage(false);
			});
		}
		
		
		function showMessage(loading) {
			var end = pullkeys[pullkeys[0]] <= 0;
			
			isLoading = loading;
			
			doc.getElementById('loading-icon').style.display = loading ? '' : 'none';
			doc.getElementById('loading-text').style.display = loading ? '' : 'none';
			
			doc.getElementById('loading-more').style.display = loading || end ? 'none' : '';
			doc.getElementById('loading-end').style.display = !loading && end ? '' : 'none';
		}
		
		
		function insertHtml(dom, html, reset) {
			if (!reset && dom.firstElementChild) {
				var div = document.createElement('div');
				var any;
				
				div.innerHTML = html;
				
				while (any = div.firstChild) {
					dom.appendChild(any);
				}
			} else {
				dom.innerHTML = html;
			}
		}
		
		
	});
	
	
}(mui, document));